package com.hzit.ssm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.hzit.ssm.mapper.BillMapper;
import com.hzit.ssm.model.BillInfo;
import com.hzit.ssm.service.IUpLoadService;
import com.hzit.ssm.vo.BillExcelVo;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 上传文件的实现类
 */
@Service

public class UpLoadServiceImpl implements IUpLoadService {

    @Autowired
    private BillMapper billMapper;


    @Override
    @Transactional
    public void upload(String filePath) {


        //EasyExcel解析Excel
        final List<BillInfo> excelVoList = new ArrayList<BillInfo>();

        EasyExcel.read(filePath, BillExcelVo.class, new AnalysisEventListener<BillExcelVo>() {

            // 每解析一条数据都会调用该方法
            @Override
            public void invoke(BillExcelVo billExcelVo, AnalysisContext analysisContext) {

                BillInfo billInfo = new BillInfo();
                BigDecimal amt = null;
                if (!StringUtils.isEmpty(billExcelVo.getAmount())){

                     amt = new BigDecimal(billExcelVo.getAmount().replace("`",""));

                }

                BeanUtils.copyProperties(billExcelVo,billInfo);

                billInfo.setAmount(amt);

                excelVoList.add(billInfo) ;
            }

            // 解析完毕的回调方法
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                System.out.println("excel文件读取完毕！");
            }

        }).sheet().doRead();

        System.out.println("excelVoList = " + excelVoList.size());

        if (CollectionUtils.isEmpty(excelVoList)){
            return ;
        }

        //批量插入
        billMapper.addBill(excelVoList);


    }

    @Override
    public List<BillExcelVo> queryAllBill() {

        //创建list
        List<BillExcelVo>  billExcelVoList = new ArrayList<BillExcelVo>();

        //调用dao层查询所有数据
        List<BillInfo> billInfoList = billMapper.queryAllBill();

        //判断是否为空
        if (CollectionUtils.isEmpty(billInfoList)){
            return billExcelVoList;
        }


        //把数据库所有数据，放入新的list
        for (BillInfo billInfo : billInfoList){

            BillExcelVo billExcelVo = new BillExcelVo();

            BeanUtils.copyProperties(billInfo,billExcelVo);

            //
            String amt = billInfo.getAmount() != null ?billInfo.getAmount().toString():"0";

            billExcelVo.setAmount(amt);

            billExcelVoList.add(billExcelVo);

        }

        return  billExcelVoList;

    }



    @Override
    public void csvUpload(String filePath) {

        //EasyExcel解析Excel
        final List<BillInfo> excelVoList = new ArrayList<BillInfo>();


        InputStreamReader is = null;
        try {
            is = new InputStreamReader(new FileInputStream(filePath), "utf-8");

            CSVParser csvParser = new CSVParserBuilder().withSeparator('\t').build();
            CSVReader reader = new CSVReaderBuilder(is).withCSVParser(csvParser).build();
            List<String[]> strings = reader.readAll();

            for (String[] s : strings){
                BillInfo billInfo = new BillInfo();

                String  orderId = s[0];
                String  amount = s[1];
                String  date = s[2];

                billInfo.setOrderId(orderId);


                excelVoList.add(billInfo);

            }


            billMapper.addBill(excelVoList);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (CsvException e) {
            e.printStackTrace();
        }


    }


}
